package com.huynhchihieu.javacore;


class UseCounter implements Runnable {
	static final Object lock = new Object();
	public void increment() {
		// increments the counter and prints the value
		// of the counter shared between threads
		
		// create block code using synchronized and then only thread
		//  acquire a lock on the this reference before reading and writing to Counter.count
		synchronized (UseCounter.lock) {
			Counter.count++;
			System.out.print(Counter.count + " ");
		}
		
	}

	public void run() {
		increment();
		increment();
		increment();
	}
}

// This class creates three threads
public class DataRace {
	public static void main(String args[]) {
		UseCounter c = new UseCounter();
		UseCounter c1 = new UseCounter();
		UseCounter c2 = new UseCounter();
		Thread t1 = new Thread(c);
		Thread t2 = new Thread(c1);
		Thread t3 = new Thread(c2);
		t1.start();
		t2.start();
		t3.start();
	}
}